﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.DataSourcesRaster;

using PlanningGIS.Win.Controls;

namespace PlanningGIS.DataConvert.model
{
    /// <summary>
    ///作用：栅格数据转换类
    ///作者：张利勇
    ///编写日期：2009-05-13
    /// </summary>
    public class RasterDataConvert
    {
        public IRasterDataset RasterConvert(IRasterLayer rasterLayer, IWorkspace pRasterWorkspace, String FileName, String FormatType)
        {
            IRasterLayerExport2 rLayerExport = new RasterLayerExportClass();
            rLayerExport.RasterLayer = rasterLayer;// rasterLayer指当前加载的栅格图层
            rLayerExport.Force2RGB = true;

            //rLayerExport.Extent = clipExtent;//clipExtent指提取栅格数据的范围
            //if (proSpatialRef != null)rLayerExport.SpatialReference = proSpatialRef;// proSpatialRef当前栅格数据的投影信息
            
            try
            {
                IRasterDataset outGeoDataset = rLayerExport.Export(pRasterWorkspace, FileName, FormatType);
                return outGeoDataset;
            }
            catch (Exception ex)
            {
                MsgHelper.ShowWarningMsgbox(ex.Message);
                return null;
            }
        }
        /// <summary>
        /// 图像数据另存为
        /// </summary>
        /// <param name="pRaster"></param>
        /// <param name="_folder"></param>
        /// <param name="FileName"></param>
        /// <param name="FormatType"></param>
        /// <param name="bAddToMap"></param>
        public IRasterDataset SaveAs(IRaster pRaster, IWorkspace pRasterWorkspace, /*String _folder,*/ String FileName, String FormatType)
        {
            IWorkspaceFactory pWF = new RasterWorkspaceFactoryClass();
            ISaveAs pSaveAs = null;

            //try
            //{
                //IWorkspace pRasterWorkspace = pWF.OpenFromFile(_folder, 0);// _folder指栅格文件保存路径
                pSaveAs = (ISaveAs)pRaster;

                IRasterDataset pRasterDataset = pSaveAs.SaveAs(FileName, pRasterWorkspace, FormatType) as IRasterDataset;

                return pRasterDataset;
            //}
            //catch (Exception ex)
            //{
            //    MsgHelper.ShowWarningMsgbox(ex.Message);
            //    return null;
            //}
        }
    }
}
